var settingTree;
var backnode;
// 动态设置树的参数
function settingTreeFunction(chkboxFather, chkboxChild) {

	settingTree = {
		view : {
			selectedMulti : true
		// 设置是否允许同时选中多个节点
		},
		edit : {
			enable : false, // 设置 zTree 是否处于编辑状态
			editNameSelectAll : false, // 节点编辑名称 input 初次显示时,设置 txt 内容是否为全选状态
		},
		data : {
			simpleData : {
				enable : true
			// 确定 zTree 初始化时的节点数据、异步加载时的节点数据、或 addNodes 方法中输入的 newNodes
			// 数据是否采用简单数据模式 (Array)
			}
		},
		check : {
			chkStyle : "checkbox",// 勾选框类型
			enable : true, // 设置 zTree 的节点上是否显示 checkbox
			chkboxType : {
				"Y" : chkboxFather,
				"N" : chkboxChild
			}
		},
		async : {
			enable : true,
			url : "../sysOrgController/loadOrgTreeData",
			autoParam : [ "id=pid" ]
		}

	};

}
//添加数据权限
var orgs=new Array();
var infos=new Array();
var check="";
var orgcheck="";
function add(){
	$('#view') .modal('show');
	$('#dataPermissionTypenew').empty();
	$('#introduction').val("");
	$('#mdiframe').attr("src","");
	$.ajax({
		url:"../dataPermissionController/getType",
		dataType:"json",
		type:"post",
		success:function(data){
			$('#dataPermissionTypenew').append('<option value="" disabled selected></option> ');
			for (var i = 0; i < data.length; i++) {
				$('#dataPermissionTypenew').append("<option value='"+data[i].ID+"' addUrl="+data[i].ADD_URL+">"+data[i].LABEL+"</option>")
			}
			
		}
	})
	$("#dataPermissionTypenew").on("change",function(){
		var addurl=prc+$("option:selected",this).attr('addUrl')+"?t="+(new Date().getTime());
		$('#mdiframe').attr("src",addurl);
	})

}
function getOrg(){
	$('#OrgModal').modal('show');
	Branch();
}
function Branch() {
	if ($("#linkBranch").is(':checked')) {
		var chkboxFather = "ps";
		var chkboxChild = "ps";
		// 设置树的参数
		settingTreeFunction(chkboxFather, chkboxChild);

	} else {
		var chkboxFather = "";
		var chkboxChild = "";
		settingTreeFunction(chkboxFather, chkboxChild);
	}
	$.ajax({
		url : "../sysOrgController/loadOrgTreeData",
		data : {
			"pid" : null
		},
		dataType : "json",
		type : "post",
		success : function(data) {
			// 加载组织树
			sysOrgTree = $.fn.zTree.init($("#orgtree"), settingTree, data);
		}
	})

}
function getOrgCheck() {
	var treeObj = $.fn.zTree.getZTreeObj("orgtree");
	var nodes = treeObj.getCheckedNodes(true);
	var node = treeObj.getCheckedNodes(true);
	backnode=new Array();
	if ($("#linkBranch").is(':checked')) {
		var t = 0;
		for (var i = 0; i < node.length; i++) {
			if (node[i].getCheckStatus().half) {
				nodes.splice(i - t, 1);
				t = t + 1;
			}
		}
		var infos = new Array();
		for (var i = 0; i < nodes.length; i++) {
			var pid = nodes[i].pId;
			if (pid != null) {
				pid = pid.toString();
				if (treeObj.getNodeByParam("id", pid, null).getCheckStatus().half == true) {
					infos.push(nodes[i]);
				}
			} else {
				infos.push(nodes[i]);
			}
		}
		return infos;
	} else {
		var info = new Array();
		for (var i = 0; i < nodes.length; i++) {
			info.push(nodes[i]);
		}
		return info;
	}
}
function selectOrg() {
	var nodes;
	var ifck;
	var checked;// 标识选中的树结构（关联下级，关联人员）
		if ($("#linkBranch").is(':checked')) {
			orgcheck = "Branch";
			ifck="(关联下级)";
		} else {
			orgcheck = "notBranch";
			ifck="(本级组织)";
		}
		nodes = getOrgCheck();
		if(nodes==null||nodes==""){
			alertModel("请选择组织范围");
			return ;
		}
		for (var i = 0; i < nodes.length; i++) {
			orgs.push(nodes[i].id);
			$("#list2").prepend('<li><div  class="divout">'+nodes[i].name+ifck+'</div></li>');
		}
	$('#OrgModal') .modal('hide');
}
function getSelectDP(nodes,checked,backnode){
	infos.push(nodes);
	check=checked;
	var name=new Array();
	for (var i = 0; i < backnode.length; i++) {
		name.push(backnode[i].name);
	}
	$("#list1").prepend('<li><div  class="divout"><div class="innertop">'+$("#dataPermissionTypenew  option:selected").text()+'</div>'+name.join()+'</div></li>');
}
function saveDP(){
	var dataIntroduction = $("#introduction").val();
	var dataDictionaryId = $("#dataPermissionTypenew").val();
	if(orgs==null||orgs==""){
		alertModel("请选择授权人员");
		return ;
	}
	if(infos==null||infos==""||check==null||check==""){
		alertModel("请选择授权范围");
		return ;
	}
		$.ajax({
			url : "../dataPermissionController/saveMassDpByOrg?t="+(new Date().getTime()),
			data : {
				"dataDictionaryId" : dataDictionaryId,
				"binds" : orgs,
				"range" : infos,
				"checked" : check,
				"orgcheck":orgcheck,
				"dataIntroduction" : dataIntroduction
			},
			traditional : true,
			dataType : "json",
			type : "post",
			success : function(data) {
				if(confirm("保存成功,是否离开？")){
					window.close();
				}else{
					window.location.reload();
				}
			}
		})
		
}

